#include <iostream>
#include "network/TcpSocket.h"
#include "Thread.hpp"

using namespace std;

class MyThread: public sh::Thread
{
public:
    explicit MyThread(sh::TcpSocket *tcpSocket)
        : tcpSocket(tcpSocket)
    {}

protected:
    void run() override
    {
        while (true)
        {
            std::string str = tcpSocket->readData(12);
            cout << str << endl;
        }
    }
private:
    sh::TcpSocket *tcpSocket;
};

int main()
{
    sh::TcpSocket tcpSocket;
    tcpSocket.connectToHost("192.168.2.189", 8080);
    if (tcpSocket.waitForConnected(6000))
    {
        MyThread myThread(&tcpSocket);
        myThread.start();
        cout << "connect pass" << endl;
        while (true)
        {
            tcpSocket.writeData("123");
            MyThread::msleep(500);
        }
    }
    else
    {
        cout << "connect fail" << endl;
    }

    return 0;
}
